package com.xcdh.trading.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xcdh.trading.entity.AStock;
import com.xcdh.trading.entity.DailyIndicators;
import com.xcdh.trading.entity.IndustryCapitalFlow;
import com.xcdh.trading.mapper.DailyIndicatorsMapper;
import com.xcdh.trading.response.TradingRes;
import com.xcdh.trading.service.AStockService;
import com.xcdh.trading.service.DailyIndicatorsService;
import com.xcdh.trading.util.TuShareApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 每日指标服务实现类，继承 ServiceImpl 实现具体业务逻辑
 */
@Service
public class DailyIndicatorsServiceImpl extends ServiceImpl<DailyIndicatorsMapper, DailyIndicators> implements DailyIndicatorsService {

    @Autowired
    private AStockService aStockService ;

    @Override
    public void syncDailyIndicators(String tradeDate) {
        TradingRes tradingRes = TuShareApi.post("daily_basic", tradeDate);
        List<DailyIndicators> indicatorsList = tradingRes.parse(DailyIndicators.class);
        for (DailyIndicators indicators : indicatorsList) {
            DailyIndicators dbData = lambdaQuery()
                    .eq(DailyIndicators::getTsCode, indicators.getTsCode())
                    .eq(DailyIndicators::getTradeDate, indicators.getTradeDate()).one();
            AStock aStock = aStockService.getById(indicators.getTsCode());
            indicators.setName(aStock.getName());
            if(dbData == null){
                save(indicators) ;
            }else{
                indicators.setId(dbData.getId());
                updateById(indicators) ;
            }
        }
    }
    // 可在此实现自定义业务逻辑方法
}
